home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14838 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  86 lines

  1. Path: news.bs.tpl.net!news
  2. From: Dieter Lⁿcking <luecking@braunschweig.netsurf.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: algorithm
  5. Date: Tue, 02 Apr 1996 06:51:29 +0100
  6. Organization: IRD Daten- und Netzwerktechnik
  7. Message-ID: <3160C061.6A7@braunschweig.netsurf.de>
  8. NNTP-Posting-Host: plueckin.braunschweig.netsurf.de
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. An algorithm:
  15.  
  16. // ============================================================================================
  17. // Contents:
  18. // ============================================================================================
  19. //
  20. // xtExchange
  21. //
  22. // ============================================================================================
  23. // (Created) Dieter Luecking
  24. // ============================================================================================
  25.  
  26.    #include <assert.h>
  27.  
  28.  
  29. // ============================================================================================
  30. // xtEchange
  31. // ============================================================================================
  32. //
  33. // Arguments:     'TYPE* start' is the stating position.
  34. //                'const TYPE* mark' is the marked position (condition: 'start' <= 'mark').
  35. //                'const TYPE* end' is the ending positin (condition: 'mark' <= 'end').
  36. // Description:   This function exchanges all values in front of 'mark' with all values
  37. //                at and after 'mark' upto 'end'.
  38. // Return:        A pointer to the new position of of 'start'.
  39. // Example:       Array: "start XXX mark YYY end ZZZ"
  40. //                Exchanged: "mark YYY start XXX end ZZZ"
  41. // Remarks:       The execution time is (in the worst case) linear to 'end' - 'start'.
  42. //                However 'TYPE::opertor == (const T&)' is invoked twice and may reduce
  43. //                the performance.
  44. //
  45. // ============================================================================================
  46.  
  47.    template <class TYPE>
  48.    TYPE* xtExchange(TYPE* start, const TYPE* mark, const TYPE* end) {
  49.       assert(start <= end && star <= mark && mark <= end);
  50.       TYPE* p = start;
  51.       TYPE* newpos = p + (end - mark);
  52.       TYPE* s = (TYPE*)mark;
  53.       TYPE  t;
  54.       while(mark < end) {
  55.          while(p < mark && s < end) {
  56.             t     = *s;
  57.             *p++  = *s;
  58.             *s++  =  t;
  59.          }
  60.          if(p == mark) mark = s;
  61.          else s = (TYPE*)mark;
  62.       }
  63.       return newpos;
  64.    }
  65.  
  66.    template <class T>
  67.    inline TYPE* xtExchange(TYPE* start, const TYPE* mark, size_t size) {
  68.       return xtExchange(start, mark, start + size; }
  69.  
  70.    template <class T>
  71.    inline TYPE* xtExchange(TYPE* start, size_t mark, size_t size) {
  72.       return xtExchange(start, start + mark, start + size); }
  73.  
  74.  
  75. // ============================================================================================
  76. // EOF
  77. // ============================================================================================
  78.  
  79. I hope that more discussions refer to programming.
  80.         Have a nice work
  81.                 Dieter Luecking
  82.  
  83. P.S.:
  84. Respose to luecking@braunschweig.netsurf.de
  85.  
  86.